﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoinDev.Models;
using CoinDev.Models.JSON;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Collections;
using System.IO;

namespace CoinDev.Controllers
{
    public class CategoryController : Controller
    {
        public ActionResult Index()
        {
            GetCategoryList();
            ArrayList paramList = new ArrayList
            {
            };

            var dtGetCategories = Data.Connections.GetDataTable("General_Get_CategoryList", paramList, "ApplicationServices");

            var Categories = new Models.JSON.CategoriesArray { };

            foreach (DataRow drGetCategories in dtGetCategories.Rows)
            {
                Categories.Category.Add(new Models.JSON.Categories
                {
                    Id = drGetCategories["Id"].ToString(),
                    Name = drGetCategories["Name"].ToString(),
                    Description = drGetCategories["Description"].ToString(),
                    Year = drGetCategories["Year"].ToString(),
                    PictureLocation = drGetCategories["Picture"].ToString()
                });
            }

            return View("", Categories);
        }
        public ActionResult Item()
        {
            GetCategoryList();
            ArrayList paramList = new ArrayList
            {
                new SqlParameter("CategoryId", Request.Params["Id"])
            };

            var dtGetItems = Data.Connections.GetDataTable("General_Get_ItemByCategoryId", paramList, "ApplicationServices");

            var Items = new Models.JSON.ItemsArray { };

            foreach (DataRow drGetItems in dtGetItems.Rows)
            {
                Items.Item.Add(new Models.JSON.Items
                {
                    Id = drGetItems["Id"].ToString(),
                    CategoryId = drGetItems["CategoryId"].ToString(),
                    Name = drGetItems["Name"].ToString(),
                    Description = drGetItems["Description"].ToString(),
                    Year = drGetItems["Year"].ToString(),
                    Price = drGetItems["Price"].ToString(),
                    PictureLocation = drGetItems["Picture"].ToString()
                });
            }

            return View("Item", Items);
        }

        public void GetCategoryList()
        {
            ArrayList Params = new ArrayList
            {
            };

            var dtCategories = Data.Connections.GetDataTable("Admin_Get_CategoryListDDL", Params, "ApplicationServices");

            var CategoryDDL = new List<SelectListItem>();

            CategoryDDL.Add(new SelectListItem { Text = "- Select -", Value = "" });

            foreach (DataRow drCategories in dtCategories.Rows)
            {
                CategoryDDL.Add(new SelectListItem { Text = drCategories["Name"].ToString(), Value = drCategories["Id"].ToString() });
            }

            ViewData["CategoryDDL"] = CategoryDDL;
        }
    }
}
